package sparrow.common;

import sparrow.indexer.Indexer;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ArrayList;

import sparrow.indexer.Indexer;

public class TopicHistory {

	private Indexer indexer;
	private Hashtable<Integer, Topic> uniqueLabels;
	private Hashtable<Integer, ChatMessage> messages;
	private Hashtable<String, LinkedList<ChatMessage>> taggedMessages;
	private int defaultNum = 1;
	private final int defaultColor = 0x3399FF;
	private int lastColor = defaultColor;

	private String username;


	public TopicHistory(String username) {
		this.username = username;
		this.indexer = new Indexer();
		this.uniqueLabels = new Hashtable<Integer,Topic>();
		this.messages = new Hashtable<Integer, ChatMessage>();
		this.taggedMessages = new Hashtable<String, LinkedList<ChatMessage>>();
	}
	
	public int getLastColor() {
		return lastColor;
	}

	public void addMessage(String msg, String date, int id) {
		ArrayList<String> tags = indexer.indexes(msg);

		ChatMessage chatMsg = new ChatMessage(username, msg, date, id, tags);
		messages.put(id, chatMsg);
		if (tags != null) {
			for (String tag : tags) {
				if (taggedMessages.containsKey(tag)) {
					taggedMessages.get(tag).add(chatMsg);
				} else {
					LinkedList<ChatMessage> msgList = new LinkedList<ChatMessage>();
					msgList.add(chatMsg);
					taggedMessages.put(tag, msgList);
				}
			}
		}
	}

	public void addTopic(String label, int id) {
		Topic topic = new Topic(label, defaultColor);
		topic.addMessage(messages.get(id));
		uniqueLabels.put(defaultColor, topic);
		lastColor = defaultColor;
	}

	public void addTopic(int color, int id) {
		String label = "label" + defaultNum;
		Topic topic = new Topic(label, color);
		topic.addMessage(messages.get(id));
		uniqueLabels.put(color, topic);
		defaultNum+=1;
		lastColor = color;
	}

	public void addTopic(String label, int color, int id) {
		Topic topic = new Topic(label,color);
		topic.addMessage(messages.get(id));
		uniqueLabels.put(color, topic);
		lastColor = color;
	}

	public LinkedList<ChatMessage> searchMsgs(String keyword) {
		LinkedList<ChatMessage> resultList = new LinkedList<ChatMessage>();
		if (uniqueLabels.containsKey(keyword)) {
			resultList.addAll(uniqueLabels.get(keyword).getMsgs());
		}
		if (taggedMessages.containsKey(keyword)) {
			resultList.addAll(taggedMessages.get(keyword));
		}
		return resultList;
	}

	public Topic findTopic(int color) {
		if (uniqueLabels.containsKey(color)) {
			lastColor = color;
			return uniqueLabels.get(color);
		}
		return null;
	}

	public Iterator<Topic> getTopics() {
		return uniqueLabels.values().iterator();
	}

	/*public boolean setLabel(String label, Topic topic) {
		if (uniqueLabels.containsKey(label)) {
			return false;
		} else if (uniqueLabels.contains(topic.label())) {
			uniqueLabels.remove(topic.label());
			uniqueLabels.put(label, topic);
			topic.setLabel(label);
			return true;
		} else {
			return false;
		}
	}*/
}
